package dictservicelogic

import (
	"context"
	"database/sql"
	"time"

	"github.com/blog-zero/rpc/model/sysmodel"
	"github.com/blog-zero/rpc/sys/internal/svc"
	"github.com/blog-zero/rpc/sys/sysclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type DictChildenUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewDictChildenUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DictChildenUpdateLogic {
	return &DictChildenUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *DictChildenUpdateLogic) DictChildenUpdate(in *sysclient.DictChildenUpdateReq) (*sysclient.DictChildenUpdateResp, error) {
	// 更新之前查询记录是否存在
	_, err := l.svcCtx.DictChildenModel.FindOne(l.ctx, in.Id)
	if err != nil {
		return nil, err
	}

	err = l.svcCtx.DictChildenModel.Update(l.ctx, &sysmodel.SysDictChilden{
		Id:          in.Id,
		ParentId:    in.ParentId,
		Label:       in.Label,
		Value:       in.Value,
		Description: in.Description,
		Sort:        float64(in.Sort),
		// CreateBy:    DictChilden.CreateBy,
		// CreateTime:  DictChilden.CreateTime,
		UpdateBy:   sql.NullString{String: in.UpdateBy, Valid: true},
		UpdateTime: time.Now(),
		Remarks:    sql.NullString{String: in.Remarks, Valid: true},
		Status:     in.Status,
	})

	if err != nil {
		return nil, err
	}

	return &sysclient.DictChildenUpdateResp{}, nil
}
